<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebUi - Login</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f2f5;
        }

        .login-container {
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            background-color: white;
            max-width: 400px;
            min-width: 300px;
            position: relative;
        }

        .input-group {
            margin-bottom: 20px;
        }

        input[type="text"] {
            width: 90%;
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
            transition: all 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }

        .error-message {
            color: red;
            margin-top: 5px;
        }
    </style>
</head>

<body>
    <div class="login-container">
        <h2>WebUi Login</h2>
        <form id="token-form" onsubmit="event.preventDefault(); submitToken();">
            <div class="input-group">
                <label for="token-input">Enter Token:</label>
                <input type="text" id="token-input" required>
            </div>
            <p class="error-message hidden" id="error-message"></p>
            <button type="submit">Login</button>
        </form>
    </div>
    <script>
        //待封装整理
        async function submitToken() {
            const tokenInput = document.getElementById('token-input');
            const errorMessage = document.getElementById('error-message');

            if (tokenInput.value.trim() === '') {
                errorMessage.textContent = 'Please enter a token.';
                errorMessage.classList.remove('hidden');
            } else {
                errorMessage.textContent = '';
                errorMessage.classList.add('hidden');
                //请求 /api/login post token
                let data = "";

                try {
                    let loginResponse = await fetch('/api/auth/login', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ token: tokenInput.value })
                    });
                    const loginResponseJson = await loginResponse.json();
                    let retCode = loginResponseJson.code;
                    if (retCode === 0) {
                        //登录成功
                        let retCredential = loginResponseJson.data.Credential;
                        localStorage.setItem('auth', retCredential);
                        let QQLoginResponse = await fetch('/api/QQLogin/CheckLoginStatus', {
                            method: 'POST',
                            headers: {
                                'Authorization': "Bearer " + retCredential,
                                'Content-Type': 'application/json'
                            }
                        });
                        if (QQLoginResponse.status == 200) {
                            let QQLoginResponseJson = await QQLoginResponse.json();
                            if (QQLoginResponseJson.code == 0) {
                                //alert(QQLoginResponseJson.data.isLogin.toString());
                                if (QQLoginResponseJson.data.isLogin) {
                                    window.location.href = './config.html';
                                } else {
                                    window.location.href = './QQLogin.html';
                                }
                            }
                        }
                        alert("登录成功即将跳转");
                    } else {
                        console.log(loginResponseJson.message);
                        alert(loginResponseJson.message);
                    }

                } catch (e) {
                    alert("登录失败");
                    console.log("请求异常", e);
                }

            }
        }
    </script>
</body>

</html>